﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace GameLib {
    public partial class Mod : IMod {
        private List<IModConflict> conflicts = new List<IModConflict>();
        private List<IModDependency> dependencies = new List<IModDependency>();
        private List<IModMessage> messages = new List<IModMessage>();

        public string Id {
            get;
            set;
        }

        public string Title {
            get;
            set;
        }

        public string File {
            get;
            set;
        }

        public string Uri {
            get;
            set;
        }

        public bool Obsolete {
            get;
            set;
        }

        public bool Patch {
            get;
            set;
        }

        public void addConflict(IModConflict conflict) {
            lock (conflicts) {
                conflicts.Remove(conflict);
                conflicts.Add(conflict);
            }
        }

        public void addDependency(IModDependency dependency) {
            lock (dependencies) {
                dependencies.Remove(dependency);
                dependencies.Add(dependency);
            }
        }

        public void addMessage(IModMessage message) {
            lock (messages) {
                messages.Remove(message);
                messages.Add(message);
            }
        }

        public int countConflicts() {
            return conflicts.Count;
        }

        public int countDependencies() {
            return dependencies.Count;
        }

        public int countMessages() {
            return messages.Count;
        }

        public IModConflict getConflict(int index) {
            return conflicts[index];
        }

        public IModDependency getDependency(int index) {
            return dependencies[index];
        }

        public IModMessage getMessage(int index) {
            return messages[index];
        }

        public void removeConflict(int index) {
            lock (conflicts) {
                conflicts.RemoveAt(index);
            }
        }

        public void removeConflict(IModConflict conflict) {
            lock (conflicts) {
                conflicts.Remove(conflict);
            }
        }

        public void removeDependency(int index) {
            lock (dependencies) {
                dependencies.RemoveAt(index);
            }
        }

        public void removeDependency(IModDependency dependency) {
            lock (dependencies) {
                dependencies.Remove(dependency);
            }
        }

        public void removeMessage(int index) {
            lock (messages) {
                messages.RemoveAt(index);
            }
        }

        public void removeMessage(IModMessage message) {
            lock (messages) {
                messages.Remove(message);
            }
        }

        public IMod Clone() {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, this);
            ms.Flush();
            ms.Position = 0;
            return (IMod) bf.Deserialize(ms);
        }
    }
}
